package com.aboo.service;

import com.aboo.po.Company;
import com.aboo.vo.ReturnVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Slf4j
@Component
public class RCompanyService {

    @Resource
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "getFallback")
    public ReturnVO<Company> get(String id) {
        String url = "http://eureka-client-two/company/get/{id}";
        ReturnVO<Company> rv = restTemplate.getForObject(url, ReturnVO.class, "5678");
        return rv;
    }

    public ReturnVO<Company> getFallback(String id) {
        log.warn("RestTemplate网络不好，熔断...");
        ReturnVO<Company> rv = new ReturnVO<>();
        rv.setCode("001");
        rv.setMsg("网络不好");
        return rv;
    }

}
